package edu.tjpu.share.newui;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import edu.tjpu.share.app.Fields;
import edu.tjpu.share.po.Grade;
import edu.tjpu.share.po.Server;
import edu.tjpu.share.po.UserVo;
import edu.tjpu.share.task.GetAcademyTask;
import edu.tjpu.share.util.ListUtil;

public class GetAcademy extends Activity {

	private Server serverinfo;
	private ListView listview;
	private static List<Grade> AcademyList;
	private int[] ARelation;

	ProgressDialog progressDialog;

	// 新消息
	private boolean isReg;
	// private List<FileForUpload> upfiles;
	private UserVo user;

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();

		serverinfo = (Server) bundle.getSerializable(Fields.SERVER_INFO);

		// 新消息
		isReg = (Boolean) bundle.getSerializable(Fields.IS_REG);
		if (!isReg) {
			// upfiles =
			// (List<FileForUpload>)bundle.getSerializable(Fields.FILE_LIST);
			user = (UserVo) bundle.getSerializable(Fields.USER);
		}

		AcademyList = null;

		progressDialog = ProgressDialog.show(GetAcademy.this, "请稍等...",
				"正在连接服务器...", true);

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put(Fields.SERVER_INFO, serverinfo);
		map.put(Fields.HANDLER, handler);

		new GetAcademyTask().execute(map);

	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == Fields.NETWORK_CONNECTION_ERROR) {
				progressDialog.dismiss();
				Toast.makeText(GetAcademy.this, "网络错误！",
						Toast.LENGTH_SHORT).show();
			} else {
				if (AcademyList != null) {
					String[] Academys = new String[AcademyList.size()];

					Iterator<Grade> academyIterator = AcademyList.iterator();
					int i = 0;
					while (academyIterator.hasNext()) {
						Academys[i++] = academyIterator.next().getGname();
					}

					ARelation = ListUtil.getGradeIDRelation(AcademyList);

					listview = new ListView(GetAcademy.this);
					listview.setAdapter(new ArrayAdapter<String>(
							GetAcademy.this,
							android.R.layout.simple_expandable_list_item_1,
							Academys));

					progressDialog.dismiss();

					listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1,
								int arg2, long arg3) {

							int gid = ARelation[arg2];

							Intent intent = new Intent();
							Bundle bundle = new Bundle();
							bundle.putSerializable(Fields.GID, gid);
							bundle.putSerializable(Fields.SERVER_INFO,
									serverinfo);

							// 新消息

							if (!isReg) {
								// bundle.putSerializable(Fields.FILE_LIST,
								// (Serializable)upfiles);
								bundle.putSerializable(Fields.USER, user);
							}
							bundle.putSerializable(Fields.IS_REG, isReg);

							intent.putExtras(bundle);

							intent.setClass(GetAcademy.this, GetMajor.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // 清除该进程空间的所有Activity
							startActivity(intent);
							// finish();
						}
					});

					GetAcademy.this.setContentView(listview);

				} else {
					progressDialog.dismiss();
					Toast.makeText(GetAcademy.this, "服务器连接失败！",
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	};

	public static void setAcademyList(List<Grade> academyList) {
		AcademyList = academyList;
	}

	/*
	 * @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
	 * 
	 * // 按下键盘上返回按钮 if (keyCode == KeyEvent.KEYCODE_BACK) {
	 * 
	 * new AlertDialog.Builder(this) .setTitle("警告") .setMessage("您确定要退出吗？")
	 * .setNegativeButton("取消", new DialogInterface.OnClickListener() {
	 * 
	 * @Override public void onClick(DialogInterface dialog, int which) { } })
	 * .setPositiveButton("确定", new DialogInterface.OnClickListener() { public
	 * void onClick(DialogInterface dialog, int whichButton) { finish(); }
	 * }).show();
	 * 
	 * return true; } else { return super.onKeyDown(keyCode, event); } }
	 */

}
